/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vue;

import controleur.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import javax.swing.*;
import metier.Carte;
import metier.Element;
import metier.Fourmi;
import metier.Fourmiliere;
import metier.Position;
import metier.Reine;
import utiles.K;

/**
 *
 * @author S219
 */
public class VuePositionnerFourmilieres extends FourmizFrame implements MouseListener, ActionListener, WindowListener {

    private HashMap<JLabel, Position> gridLabel = new HashMap<JLabel, Position>();
    private final ControllerSimulation controller;
    private final Carte carte;
    private final JButton buttonRetour;
    private final JButton buttonValider;
    private final JTextField nbInput;

    public VuePositionnerFourmilieres(ControllerSimulation c) {
        super("Placement des fourmilières");
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());

        this.controller = c;
        this.carte = this.controller.getCarte();

        // Gestion de la carte
        GridLayout gridLayout = new GridLayout(this.carte.getLongueur(), this.carte.getLargeur());
        JPanel grid = new JPanel(gridLayout);
        
        for (int y = 0; y < carte.getLongueur(); y++) {
            for (int x = 0; x < carte.getLargeur(); x++) {
                ArrayList<Element> elements = this.carte.getElements(new Position(x, y));

                Color couleur = null;
                for (Element e : elements) {
                    if (e != null) {
                        switch (e.getClass().getName()) {
                            case "metier.Herbe":
                                couleur = Color.GREEN;
                                break;
                            case "metier.Roche":
                                couleur = Color.GRAY;
                                break;
                            case "metier.Nouriturre":
                                couleur = Color.YELLOW;
                                break;
                            case "metier.Obstacle":
                                couleur = Color.BLACK;
                                break;
                        }
                    }
                }

                JLabel label = new JLabel("");
                label.addMouseListener(this);

                if (couleur != null) {
                    label.setBackground(couleur);
                    label.setOpaque(true);
                }

                grid.add(label);
                this.gridLabel.put(label, new Position(x, y));
            }
        }

        // bouton retour
        this.buttonRetour = new JButton("Retour");
        buttonRetour.addActionListener(this);

        //bouton suivant
        this.buttonValider = new JButton("Valider");
        buttonValider.addActionListener(this);

        JPanel panelValider = new JPanel();
        panelValider.add(new JLabel(""));
        panelValider.add(buttonRetour);
        panelValider.add(buttonValider);
        panelValider.add(new JLabel(""));

        // conf nb fourmis
        JLabel nbText = new JLabel("Nombre de fourmis : ");
        this.nbInput = new JTextField("10");
        this.nbInput.setSize(100, 300);
        JPanel panelNbFourmis = new JPanel();
        panelNbFourmis.add(nbText);
        panelNbFourmis.add(nbInput);

        JPanel panelSud = new JPanel(new GridLayout(2, 1));
        panelSud.add(panelNbFourmis);
        panelSud.add(panelValider);

        this.add("Center", grid);
        this.add("South", panelSud);

        this.addWindowListener(this);
        this.setVisible(true);
    }
    
    protected void quitter()
    {
        this.dispose();
        new ControllerParametrageSimulation(this.carte.getEnvironnement());
    }
    
    @Override
    public void windowClosing(WindowEvent e) {
        this.quitter();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
		JLabel label = (JLabel) e.getSource();

		Position pos = this.gridLabel.get(label);
		
		if (e.getButton() == MouseEvent.BUTTON1) {
			// on ajoute la fourmiliere à la carte
			Fourmiliere f = new Fourmiliere(K.QUANTITE_NOURRITURE_INITIALE);
			this.carte.addElement(f, pos);
			this.carte.addElement(new Reine(pos, f), pos);

			// on ajoute les fourmis de base
			f.setBaseNbFourmi(Integer.parseInt(this.nbInput.getText()));
			for (int i = 0; i < f.getBaseNbFourmi() ; i++) {
				this.carte.addElement(new Fourmi(this.carte.getEnvironnement().getDureeVieFourmis(), K.QTE_NOURRITURE_TRANSPORTABLE, f), pos);
			}

			label.setOpaque(true);
			label.setBackground(Color.CYAN);
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			Fourmiliere fourmiliere = null;
			
			Color couleur = null;
			
			for (Element element : this.carte.getElements(pos)) {
				if (element instanceof Fourmiliere)
					fourmiliere = (Fourmiliere) element;
				else if (!(element instanceof Fourmi))
					couleur = element.getCouleur();
			}
			
			if (fourmiliere != null) {
				for (Fourmi fourmi : fourmiliere.getFourmis()) {
					this.carte.removeElement(fourmi);
				}
				this.carte.removeElement(fourmiliere);
				
				label.setOpaque(true);
				label.setBackground(couleur);
			}
		}
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == this.buttonValider) {
            this.dispose();
            new VueSimulation(this.controller);
        } else if (source == this.buttonRetour) {
            this.quitter();
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}
